package com.chaihu.front.service.nav.impl;

import com.alibaba.fastjson.JSONObject;
import com.chaihu.core.base.service.front.BaseComponentService;
import com.chaihu.core.context.IPageData;
import com.chaihu.front.service.login.LoginService;
import com.chaihu.front.service.nav.NavService;
import com.chaihu.utils.constant.ServiceCodeConstant;
import com.chaihu.utils.constant.ServiceConstant;
import com.chaihu.utils.util.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author chaihu
 * @date 2021/10/21
 */
@Slf4j
@Service("navServiceImpl")
public class NavServiceImpl extends BaseComponentService implements NavService {
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public ResponseEntity<String> doLogout(IPageData pd) {
        String apiUrl = ServiceConstant.SERVICE_API_URL + "/api/" + ServiceCodeConstant.SERVICE_CODE_USER_SERVICE_PC_LOGINOUT;
        ResponseEntity<String> responseEntity = null;
        JSONObject exitInfo = new JSONObject();
        exitInfo.put("token", pd.getToken());
        responseEntity = this.callCenterService(restTemplate, pd, exitInfo.toJSONString(), apiUrl, HttpMethod.POST);

        return responseEntity;
    }

    @Override
    public ResponseEntity<String> getUserInfo(IPageData pd) {
        Assert.hasLength(pd.getUserId(), "用户还没有登录");
//        super.getUserInfo(pd);

        String apiUrl = ServiceConstant.SERVICE_API_URL + "/api/" + ServiceCodeConstant.SERVICE_CODE_USER_QUERY_USER_INFO + "?userId=" + pd.getUserId();
        return this.callCenterService(restTemplate, pd, "", apiUrl, HttpMethod.GET);
    }
}
